package com.itboye.sunsunhome.www.aq;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.itboye.sunsunhome.www.aq.R;
import com.itboye.sunsunhome.www.aq.base.AqDeviceInfo;
import com.itboye.sunsunhome.www.aq.base.MyApplication;

@SuppressLint("NewApi") public class ManualAddDeviceActivity extends ActionBarActivity {

	MyApplication mApp;
	EditText mEditTextDid;
	EditText mEditTextPassword;
	EditText mEditTextName;
	Button mButtonDelete;
	Context mContext;
	ManualAddDeviceActivity mThis;
	AqDeviceInfo mDeviceInfo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_manual_add_device);
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setDisplayShowTitleEnabled(true);
		mApp = (MyApplication)getApplication();
		mApp.mManualAddDeviceUi = this;
		mContext = this;
		mThis = this;
		mDeviceInfo = new AqDeviceInfo();
		mEditTextDid = (EditText) findViewById(R.id.manual_add_device_editText1);
		mEditTextPassword = (EditText) findViewById(R.id.manual_add_device_editText2);
		mEditTextName = (EditText) findViewById(R.id.manual_add_device_editText3);
		mEditTextName.setText(mDeviceInfo.mDeviceName);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_manual_add_device, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == android.R.id.home) {
			// 返回
			mApp.mEditDeviceUi = null;
			this.finish();
			return true;
		}
		if (id == R.id.manual_add_device_action_settings1) {
			// 添加
			mDeviceInfo.mDeviceName = mEditTextName.getText().toString();
			mDeviceInfo.mDid = mEditTextDid.getText().toString();
			mDeviceInfo.mPassword = mEditTextPassword.getText().toString();
			if ( (mDeviceInfo.mDeviceName.compareTo("") == 0) ||
					(mDeviceInfo.mDid.compareTo("") == 0) ||
					(mDeviceInfo.mPassword.compareTo("") == 0)) {
				Toast.makeText(mContext, "请输入有效数据", Toast.LENGTH_SHORT).show();
				return true;
			}
			mApp.addDevice(mDeviceInfo);
			this.finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 如果是返回键
		if(keyCode == KeyEvent.KEYCODE_BACK){
			mApp.mManualAddDeviceUi = null;
		}
		return super.onKeyDown(keyCode, event);
	}
}
